package com.ruoyi.health.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.health.mapper.ActivityDetailMapper;
import com.ruoyi.health.mapper.RegionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.health.domain.ActivityDetail;
import com.ruoyi.health.mapper.ActivityMapper;
import com.ruoyi.health.domain.Activity;
import com.ruoyi.health.service.IActivityService;

/**
 * 活动风采管理Service业务层处理
 * 
 * @author dalian
 * @date 2023-08-06
 */
@Service
public class ActivityServiceImpl implements IActivityService 
{
    @Autowired
    private ActivityMapper activityMapper;

    @Autowired
    private RegionMapper regionMapper;

    @Autowired
    private ActivityDetailMapper activityDetailMapper;

    /**
     * 查询活动风采管理
     * 
     * @param id 活动风采管理主键
     * @return 活动风采管理
     */
    @Override
    public Activity selectActivityById(Long id)
    {
        return activityMapper.selectActivityById(id);
    }

    /**
     * 查询活动风采管理列表
     * 
     * @param activity 活动风采管理
     * @return 活动风采管理
     */
    @Override
    public List<Activity> selectActivityList(Activity activity) {
        //判断当前给的regionId是哪一级？
        Long regionLevel = null;
        List<Long> region3IdsList = null;
        if(activity.getRegionId() !=null){
            regionLevel = regionMapper.selectRegionLevel(activity.getRegionId());
            //查询该region 下面所有的 【三级社区】
            if(regionLevel==1){
                //根据一级id查询
                region3IdsList = regionMapper.selectRegion3ByParent1Id(activity.getRegionId());
            }else if(regionLevel ==2){
                //根据二级id查询
                region3IdsList = regionMapper.selectRegion3ByParent2Id(activity.getRegionId());
            }else{
                //只查某一个三级id
                region3IdsList = new ArrayList<>();
                region3IdsList.add(activity.getRegionId());
            }
        }

        //返回结果
        return activityMapper.selectActivityList(activity,region3IdsList);
    }

    /**
     * 新增活动风采管理
     * 
     * @param activity 活动风采管理
     * @return 结果
     */
    @Transactional
    @Override
    public int insertActivity(Activity activity)
    {
        activity.setCreateTime(DateUtils.getNowDate());
        int rows = activityMapper.insertActivity(activity);
        //插入activity详情
        insertActivityDetail(activity);
        return rows;
    }

    /**
     * 修改活动风采管理
     * 
     * @param activity 活动风采管理
     * @return 结果
     */
    @Transactional
    @Override
    public int updateActivity(Activity activity)
    {
        // 删除old详情
        activityMapper.deleteActivityDetailByActivityId(activity.getId());
        // 添加new详情
        insertActivityDetail(activity);
        return activityMapper.updateActivity(activity);
    }

    /**
     * 批量删除活动风采管理
     * 
     * @param ids 需要删除的活动风采管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteActivityByIds(Long[] ids)
    {
        activityMapper.deleteActivityDetailByActivityIds(ids);
        return activityMapper.deleteActivityByIds(ids);
    }

    /**
     * 删除活动风采管理信息
     * 
     * @param id 活动风采管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteActivityById(Long id)
    {
        activityMapper.deleteActivityDetailByActivityId(id);
        return activityMapper.deleteActivityById(id);
    }

    /**
     * 新增活动介绍信息
     * 
     * @param activity 活动风采管理对象
     */
    public void insertActivityDetail(Activity activity)
    {
        ActivityDetail activityDetail = activity.getActivityDetail();
        Long id = activity.getId();
        if (StringUtils.isNotNull(activityDetail))
        {
            //活动的详情
            activityDetail.setActivityId(id);
            activityDetailMapper.insertActivityDetail(activityDetail);

        }
    }
}
